﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Alogient.Cameleon.Sdk.Items;

namespace Alogient.Cameleon.Sdk.UI.WebControls
{
    public enum MenuStartPoint
    {
        RootPage,
        DefaultPage,
        OtherPage
    }

    [ToolboxData("<{0}:CMMenu runat=server></{0}:CMMenu>")]
    public class CMMenu : WebControl
    {
        protected CMPageCollection breadcrumb = CMServices.GetPage().Breadcrumbs;
        protected int navID = NavigationController.GetNavID();
        protected string cultureCode = NavigationController.GetCultureCode();
        
        /// <summary>
        /// Gets or sets Menu depth, default value is 2 which is the first level and submenus.
        /// </summary>
        [Category("Cameleon")]
        [Browsable(true)]
        public int Depth { get; set; }

        /// <summary>
        /// Gets or sets the start point to load the menu, takes the subpages below this page. Default value is DefaultPage.
        /// </summary>
        [Category("Cameleon")]
        [Browsable(true)]
        [DefaultValue(WebControls.MenuStartPoint.DefaultPage)]
        public MenuStartPoint MenuStartPoint { get; set; }

        /// <summary>
        /// Gets or sets the Page element used for each item text. Uses PageTitle if it doesn't exist or is not defined.
        /// </summary>
        [Category("Cameleon")]
        [Browsable(true)]
        public string TitleField { get; set; }

        [Category("Cameleon")]
        [Browsable(true)]
        public int NavID
        {
            get { return this.navID; }
            set { this.navID = value; }
        }

        public CMMenu()
        {
            this.Depth = 2;
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Ul;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.Controls.Clear();

            CMPage startPage = null;

            switch (this.MenuStartPoint)
            {
                default: break;
                case MenuStartPoint.DefaultPage:
                    {
                        startPage = CMServices.GetPage(CMSettings.DefaultNavID);
                    } 
                    
                    break;
                case MenuStartPoint.RootPage:
                    {
                        startPage = CMServices.GetPage(0);
                    } 
                    
                    break;
                case MenuStartPoint.OtherPage:
                    {
                        startPage = CMServices.GetPage(this.NavID);
                    } 
                    
                    break;
            }

            if (startPage == null)
            {
                return;
            }

            WebControl emptyWebControl = null;
            foreach (var subpage in startPage.Subpages)
            {
                if (!subpage.PageNavEnd)
                {
                    this.AddItem(ref emptyWebControl, subpage, 1);
                }
            }
        }


        protected void AddItem(ref WebControl parentMenuItem, CMPage page, int itemDepth)
        {
            WebControl item = new WebControl(HtmlTextWriterTag.Li);

            HyperLink link = new HyperLink();

            link.Text = page.PageTitle;
            if (!string.IsNullOrEmpty(this.TitleField) && page.Elements.Exists(this.TitleField))
            {
                link.Text = page.Elements[this.TitleField].Value;
            }

            link.Attributes["NavID"] = page.NavID.ToString();
            link.NavigateUrl = page.NavigateUrl;

            // Selected
            if (this.breadcrumb.GetPage(page.NavID) != null)
            {
                link.CssClass = "Selected";
            }

            item.Controls.Add(link);

            // Add item in menu
            if (parentMenuItem == null)
            {
                this.Controls.Add(item);
            }
            else
            {
                parentMenuItem.Controls.Add(item);
            }

            // Add subpages
            if (itemDepth < this.Depth)
            {
                var ulItem = new WebControl(HtmlTextWriterTag.Ul);

                foreach (var subpage in page.Subpages)
                {
                    if (!subpage.PageNavEnd)
                    {
                        this.AddItem(ref ulItem, subpage, itemDepth + 1);
                    }
                }

                if (ulItem.Controls.Count > 0)
                {
                    item.Controls.Add(ulItem);
                }
            }
        }
    }
}
